<template>
    <div>
        <!-- 搜索表单 -->
        <el-form :inline="true" :model="searchFrom" ref="searchFrom" class="demo-form-inline" style="text-align:center">
            <el-form-item label="编号" prop="id">
                <el-input v-model="searchFrom.id" placeholder="编号"></el-input>
            </el-form-item>

            <el-form-item>
                <el-button type="primary" icon="el-icon-search" @click="searchBtn()">查询</el-button>
                <el-button type="warning" icon="el-icon-refresh" @click="resetForm()">重置</el-button>
            </el-form-item>
        </el-form>

        <!-- 表格 -->
        <el-table :data="tableData" stripe style="width: 100%">
            <el-table-column prop="id" label="编号"/>
            <el-table-column prop="name" label="名称"/>
            <el-table-column prop="mpname" label="轮播位置"/>
            <el-table-column prop="pic" label="图片">
                <template slot-scope="scope">
                    <img :src="scope.row.pic" alt="" width="100px" height="100px">
                </template>
            </el-table-column>
            <el-table-column prop="startTime" label="开始时间"/>
            <el-table-column prop="endTime" label="结束时间"/>
            <el-table-column prop="status" label="状态">
                <template slot-scope="scope">
                    <span v-if="scope.row.status === 1"><el-button size="mini"  type="primary" round>上线</el-button></span>
                    <span v-if="scope.row.status === 0"><el-button size="mini" type="success" round>下线</el-button></span>
                </template>
            </el-table-column>
            <el-table-column prop="clickCount" label="点击数"/>
            <el-table-column prop="orderCount" label="下单数"/>
            <el-table-column prop="url" label="链接地址"/>
            <el-table-column prop="note" label="备注"/>
            <el-table-column prop="sort" label="排序等级"/>
            <el-table-column align="right">
                <template slot="header">
                    <span style="margin-right:30px">操作</span>
                    <el-button size="mini" type="primary" @click="openAddDialog()">添加</el-button>
                </template>
                <template slot-scope="scope">
                    <el-button size="mini" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
                    <el-button size="mini" type="danger" @click="handleDelete(scope.$index,scope.row)">删除</el-button>
                </template>
            </el-table-column>
        </el-table>

        <!-- 分页 -->
        <div class="block" style="text-align:center">
            <el-pagination
                    @size-change="handleSizeChange"
                    @current-change="handleCurrentChange"
                    :current-page="pageNum"
                    :page-sizes="[1, 2, 5, 10]"
                    :page-size="pageSize"
                    layout="total, sizes, prev, pager, next, jumper"
                    :total="total">
            </el-pagination>
        </div>

        <!-- 添加/修改弹出框 -->
        <el-dialog title="广告位置信息" :visible.sync="dialogAdd">
            <el-form :model="advertise" ref="addAdvertise" :rules="rules">
                <el-input v-model="advertise.id" style="display: none"/>
                <el-form-item label="名称" label-width="120px" prop="name">
                    <el-input v-model="advertise.name" ></el-input>
                </el-form-item>
                <!-- 下拉框 -->
                <el-form-item label="轮播位置" prop="name" label-width="120px">
                    <el-select v-model="position.id" placeholder="请选择轮播位置" style="width: 200px; margin-left: 20px">
                        <el-option :value="item.id" :key="item.id" :label="item.name"
                                   v-for="item in position"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="图片地址" label-width="120px" prop="pic">
                    <div>
                        <el-upload
                                class="avatar-uploader"
                                action="http://localhost:12188/ss/user/upload"
                                :show-file-list="false"
                                :headers="headers"
                                :on-success="handleAvatarSuccessByReverse"
                                :before-upload="beforeAvatarUpload">
                            <img v-if="advertise.pic" :src="advertise.pic" class="avatar" height="100px" width="100px">
                            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                        </el-upload>
                    </div>
                </el-form-item>
                <el-form-item label="开始时间" label-width="120px" prop="startTime">
                    <el-col :span="11">
                        <el-form-item prop="startTime">
                            <el-date-picker type="datetime"
                                            format="yyyy-MM-dd"
                                            value-format="yyyy-MM-dd HH:mm:ss"
                                            placeholder="选择日期" v-model="advertise.startTime"
                                            style="width: 100%;"></el-date-picker>
                        </el-form-item>
                    </el-col>
                </el-form-item>
                <el-form-item label="结束时间" label-width="120px" prop="endTime">
                    <el-col :span="11">
                        <el-form-item prop="endTime">
                            <el-date-picker type="datetime"
                                            format="yyyy-MM-dd"
                                            value-format="yyyy-MM-dd HH:mm:ss"
                                            placeholder="选择日期" v-model="advertise.endTime"
                                            style="width: 100%;"></el-date-picker>
                        </el-form-item>
                    </el-col>
                </el-form-item>
                <el-form-item label="状态" label-width="120px" prop="status">
                    <el-radio-group v-model="advertise.status">
                        <el-radio :label="0" border>下线</el-radio>
                        <el-radio :label="1" aria-checked="true" border>上线</el-radio>
                    </el-radio-group>
                </el-form-item>
                <el-form-item label="点击数" label-width="120px" prop="clickCount">
                    <el-input v-model="advertise.clickCount" ></el-input>
                </el-form-item>
                <el-form-item label="下单数" label-width="120px" prop="orderCount">
                    <el-input v-model="advertise.orderCount" ></el-input>
                </el-form-item>
                <el-form-item label="链接地址" label-width="120px" prop="url">
                    <el-input v-model="advertise.url" ></el-input>
                </el-form-item>
                <el-form-item label="备注" label-width="120px" prop="note">
                    <el-input v-model="advertise.note" ></el-input>
                </el-form-item>
                <el-form-item label="排序等级" label-width="120px" prop="sort">
                    <el-input-number v-model="advertise.sort" autocomplete="off"></el-input-number>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="dialogAdd=false">取 消</el-button>
                <el-button type="primary" @click="addAdvertiseBtn()">确 定</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
    export default {
        data() {
            return {
                tableData: [],
                //请求头
                headers: {
                    myAuthorization: window.localStorage.getItem("token"),
                },
                total: 0, // 总条数
                pageNum: 1, // 当前页数
                pageSize: 5, // 每页显示几条
                dialogAdd: false,
                searchFrom: {},
                advertise: {},  // 添加和编辑的时候的通用广告信息
                position:{},  //所有位置信息
                rules: {
                    name : [
                        {required: true, message: '名称', trigger: 'blur'},
                    ],
                    position_id: [
                        {required: true, message: '请输广告位置', trigger: 'blur'}
                    ],
                    status: [
                        {required: true, message: '请选择状态', trigger: 'blur'},
                    ],
                    sort:[
                        {required: true, message: '请输入排序等级', trigger: 'blur'},
                    ],
                },
            }
        },
        methods: {
            // 重置表格
            resetForm() {
                this.searchFrom={};
                this.getAllAdvertise();
            },
            // 添加按钮
            openAddDialog() {
                // 设置添加的初始值
                this.advertise = {};
                this.dialogAdd = true;
            },
            // 打开编辑操作
            handleEdit(index, row) {
                this.dialogAdd = true // 打开弹出层
                // 让表单重新赋值
                this.advertise = JSON.parse(JSON.stringify(row))
            },
            //每页显示几条改变
            handleSizeChange(val) {
                this.pageSize = val;
                this.getAllAdvertise();
            },
            // 当前是第几页改变
            handleCurrentChange(val) {
                this.pageNum = val;
                this.getAllAdvertise();
            },
            // 查询表单提交
            searchBtn() {
                this.pageNum = 1;
                this.getAllAdvertise();
            },
            //执行上传之后触发的方法
            handleAvatarSuccessByReverse(res, file) {
                this.advertise.pic = res.t;
            },
            //执行添加之前触发的方法
            beforeAvatarUpload(file) {
                const isJPG = file.type === 'image/jpeg';
                const isGIF = file.type === 'image/gif';
                const isPNG = file.type === 'image/png';
                const isBMP = file.type === 'image/bmp';
                const isLt2M = file.size / 1024 / 1024 < 2;

                if (!isJPG && !isGIF && !isPNG && !isBMP) {
                    this.common.errorTip('上传图片必须是JPG/GIF/PNG/BMP 格式!');
                }
                if (!isLt2M) {
                    this.common.errorTip('上传图片大小不能超过 2MB!');
                }
                return (isJPG || isBMP || isGIF || isPNG) && isLt2M;
            },
            // 获得表格数据
            getAllAdvertise() {
                this.$http.post('mk/advertise/queryAll?pageSize=' + this.pageSize + '&pageNo=' + this.pageNum, this.searchFrom)
                    .then(res => {
                        this.tableData = res.data.t.list;
                        this.total  = res.data.t.total
                    })
            },
            // 添加/修改表单提交
            addAdvertiseBtn() {
                console.log(this.advertise);
                this.$refs["addAdvertise"].validate((valid) => {
                    if (valid){
                        if (this.advertise.id === undefined){
                            this.$http.post("mk/advertise/insert", this.advertise).then(res => {
                                if (Number(res.data.resultCode) === 2000) {
                                    this.$message({type: 'success', message: '恭喜你，添加成功'});
                                    this.dialogAdd = false;
                                    this.getAllAdvertise();
                                } else {
                                    this.$message({type: 'error', message: '系统异常'});
                                }
                            })
                        }else {
                            this.$http.put("mk/advertise/update", this.advertise).then(res => {
                                if (Number(res.data.resultCode) === 2000) {
                                    this.$message({type: 'success', message: '恭喜你，修改成功'});
                                    this.dialogAdd = false;
                                    this.getAllAdvertise();
                                } else {
                                    this.$message({type: 'error', message: '系统异常'});
                                }
                            })
                        }

                    }
                })
            },
            // 删除
            handleDelete(index, row) {
                this.$confirm('此操作将永久删除该记录, 是否继续?', '提示', {
                    confirmButtonText: '确定', cancelButtonText: '取消', type: 'warning', center: true
                }).then(() => {
                    this.$http.delete("mk/advertise/deleteById?advertiseId=" + row.id).then(res => {
                        if (Number(res.data.resultCode) === 2000) {
                            this.$message({type: 'success', message: '删除成功!'});
                            this.getAllAdvertise();
                        } else {
                            this.$message({type: 'error', message: '系统异常'});
                        }
                    })
                }).catch(() => {
                    this.$message({type: 'info', message: '已取消删除'});
                });
            }
        },
        mounted() {
            this.getAllAdvertise();
            //获取所有的图片位置
            this.$http.post('mk/advertisePosition/queryAll?pageSize=1000000000&pageNo=1',{})
                .then(res => {
                    this.position = res.data.t.list;
                })
        }
    }
</script>

<style scoped>

</style>